package com.qingzhuge.member.utils;

import com.qingzhuge.autoconfigure.MessageProperties;
import com.qingzhuge.member.entity.SysUserMessage;
import com.qingzhuge.member.enums.EnumMessageSendType;
import com.qingzhuge.member.service.ISysUserMessageService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Auther: ChenQ
 * @Date: 2019/1/3 17:29
 */
@Component
public class MallSendMessages {

    @Resource
    private MessageProperties properties;

    @Resource
    private ISysUserMessageService messageService;


    public SysUserMessage sendClient(SysUserMessage mallMessage, EnumMessageSendType type, List<String> toUserId) {
        SysUserMessage message = messageService.save(mallMessage);
        boolean successfully = false;
        if (EnumMessageSendType.MESSAGE == type) {
            successfully = sendMessage(message, toUserId);
        } else if (EnumMessageSendType.NOTICE == type) {
            successfully = sendNotice(message, toUserId);
        } else
        if (EnumMessageSendType.SMS == type) {
            successfully = sendSms(message, toUserId);
        }
        //状态：发送中0，成功1，失败-1
        message.setStatus(successfully ? 1 : -1);
        return messageService.save(message);
    }

    /**
     * 自定义消息
     */
    private boolean sendMessage(SysUserMessage userMessage, List<String> toUserId) {
        List<String> aliases = setAliases(toUserId);
        GetSendContents contents = new GetSendContents(userMessage).invoke();
        return MessagePushHelper.sendMessage(properties.getAppKey(), properties.getAppSecret(), contents.getTitle(), contents.getExtraParam(),
                aliases);
    }

    /**
     * 发生通知
     */
    private boolean sendNotice(SysUserMessage userMessage, List<String> toUserId) {
        List<String> aliases = setAliases(toUserId);
        GetSendContents contents = new GetSendContents(userMessage).invoke();
        return MessagePushHelper.sendNotice(properties.getAppKey(), properties.getAppSecret(), contents.getTitle(), contents.getExtraParam(),
                aliases);
    }

    /**
     * 发生sms
     */
    private boolean sendSms(SysUserMessage userMessage, List<String> toUserId) {
        return false;
    }

    /**
     * 设置发送的用户 id
     *
     * @param toUserId 一个或多个
     */
    private List<String> setAliases(List<String> toUserId) {
        List<String> aliases = new ArrayList<>();
        if (!toUserId.isEmpty()) {
            aliases.addAll(toUserId);
        }
        return aliases;
    }

    /**
     * 发送的消息体
     */
    private class GetSendContents {
        private SysUserMessage userMessage;
        private String title;
        private HashMap<String, Object> extraParam;

        private GetSendContents(SysUserMessage userMessage) {
            this.userMessage = userMessage;
        }

        private String getTitle() {
            return title;
        }

        private HashMap<String, Object> getExtraParam() {
            return extraParam;
        }

        private GetSendContents invoke() {
            title = userMessage.getName();
            extraParam = new HashMap<>();
            extraParam.put("id", userMessage.getId().toString());
            extraParam.put("content", userMessage.getContent());
            extraParam.put("time", userMessage.getCreateTime());
            return this;
        }
    }
}